<template>
  <el-dialog
    :title="!dataForm.pileNo ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="堆位编号" prop="pileId">
      <el-input v-model="dataForm.pileId" placeholder="堆位编号"></el-input>
    </el-form-item>
    <el-form-item label="堆位名称" prop="pileName">
      <el-input v-model="dataForm.pileName" placeholder="堆位名称"></el-input>
    </el-form-item>
    <el-form-item label="堆位位置左上角X坐标" prop="pilePosX1">
      <el-input v-model="dataForm.pilePosX1" placeholder="堆位位置左上角X坐标"></el-input>
    </el-form-item>
    <el-form-item label="堆位位置左上角Y坐标" prop="pilePosY1">
      <el-input v-model="dataForm.pilePosY1" placeholder="堆位位置左上角Y坐标"></el-input>
    </el-form-item>
    <el-form-item label="堆位位置左上角Z坐标" prop="pilePosZ1">
      <el-input v-model="dataForm.pilePosZ1" placeholder="堆位位置左上角Z坐标"></el-input>
    </el-form-item>
    <el-form-item label="堆位位置右下角X坐标" prop="pilePosX2">
      <el-input v-model="dataForm.pilePosX2" placeholder="堆位位置右下角X坐标"></el-input>
    </el-form-item>
    <el-form-item label="堆位位置右下角Y坐标" prop="pilePosY2">
      <el-input v-model="dataForm.pilePosY2" placeholder="堆位位置右下角Y坐标"></el-input>
    </el-form-item>
    <el-form-item label="堆位位置右下角Z坐标" prop="pilePosZ2">
      <el-input v-model="dataForm.pilePosZ2" placeholder="堆位位置右下角Z坐标"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          pileNo: 0,
          pileId: '',
          pileName: '',
          pilePosX1: '',
          pilePosY1: '',
          pilePosZ1: '',
          pilePosX2: '',
          pilePosY2: '',
          pilePosZ2: ''
        },
        dataRule: {
          pileId: [
            { required: true, message: '堆位编号不能为空', trigger: 'blur' }
          ],
          pileName: [
            { required: true, message: '堆位名称不能为空', trigger: 'blur' }
          ],
          pilePosX1: [
            { required: true, message: '堆位位置左上角X坐标不能为空', trigger: 'blur' }
          ],
          pilePosY1: [
            { required: true, message: '堆位位置左上角Y坐标不能为空', trigger: 'blur' }
          ],
          pilePosZ1: [
            { required: true, message: '堆位位置左上角Z坐标不能为空', trigger: 'blur' }
          ],
          pilePosX2: [
            { required: true, message: '堆位位置右下角X坐标不能为空', trigger: 'blur' }
          ],
          pilePosY2: [
            { required: true, message: '堆位位置右下角Y坐标不能为空', trigger: 'blur' }
          ],
          pilePosZ2: [
            { required: true, message: '堆位位置右下角Z坐标不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.pileNo = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.pileNo) {
            this.$http({
              url: this.$http.adornUrl(`/pile/pile/info/${this.dataForm.pileNo}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.pileId = data.pile.pileId
                this.dataForm.pileName = data.pile.pileName
                this.dataForm.pilePosX1 = data.pile.pilePosX1
                this.dataForm.pilePosY1 = data.pile.pilePosY1
                this.dataForm.pilePosZ1 = data.pile.pilePosZ1
                this.dataForm.pilePosX2 = data.pile.pilePosX2
                this.dataForm.pilePosY2 = data.pile.pilePosY2
                this.dataForm.pilePosZ2 = data.pile.pilePosZ2
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/pile/pile/${!this.dataForm.pileNo ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'pileNo': this.dataForm.pileNo || undefined,
                'pileId': this.dataForm.pileId,
                'pileName': this.dataForm.pileName,
                'pilePosX1': this.dataForm.pilePosX1,
                'pilePosY1': this.dataForm.pilePosY1,
                'pilePosZ1': this.dataForm.pilePosZ1,
                'pilePosX2': this.dataForm.pilePosX2,
                'pilePosY2': this.dataForm.pilePosY2,
                'pilePosZ2': this.dataForm.pilePosZ2
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
